#include<bits/stdc++.h>
//#define int long long
#define ll long long
using namespace std;
const int maxn=1e6+9;
const ll INF=1e18;
int n;
struct node
{
	int x,y;
}a[maxn];
const int mmm=4e3+9;
vector<int> G[mmm];
int oid[mmm][mmm];
ll f[mmm][mmm];
int m;
void solve() 
{
    for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) f[i][j]=INF;
    for(int s=1;s<=n;s++)
	{
        deque<int> q;
        q.push_back(s);
        f[s][s]=0;
        while(!q.empty()) 
		{
            int u=q.front();
			q.pop_front();
            for(int v:G[u]) 
			{
                if(f[s][v]==INF) 
				{
                    f[s][v]=f[s][u] + 1;
                    q.push_back(v);
                }
            }
        }
    }
}
void adde(int x,int y) 
{
	if(x==0) return;
	m+=2;
	G[x].push_back(y),G[y].push_back(x);
}
signed main()
{
	freopen("game.in","r",stdin);
	freopen("game.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i].x>>a[i].y,oid[a[i].x][a[i].y]=i;
	if(n<=4000)
	{
		for(int i=1;i<=n;i++)
		{
			int p1=oid[a[i].x-1][a[i].y];
			int p2=oid[a[i].x+1][a[i].y];
			int p3=oid[a[i].x][a[i].y-1];
			int p4=oid[a[i].x][a[i].y+1];
			adde(p1,i),adde(p2,i),adde(p3,i),adde(p4,i);
		}
		solve();
		int ans=0;
		for(int i=1;i<=n;i++) for(int j=i;j<=n;j++) ans+=f[i][j];
		cout<<ans<<'\n';
		return 0; 
	}
	return 0;
}
